﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Reflection;
using System.Xml;
using System.IO;

using Factonomy.AzureStorageUtility.WindowsApp.Resources;

namespace Factonomy.AzureStorageUtility.WindowsApp.UI.Forms
{
    public partial class Options : Form
    {
        public Options()
        {
            InitializeComponent();

            // set the window icon
            this.Icon = ResourceManager.GetAppIcon();

            this.InitializeAccounts();

            if (Globals.RootPathSetting != null)
            {
                this.uiTextboxRootPath.Text = Globals.RootPathSetting;
            }

            if (Globals.AutoRefreshContainersSetting != null)
            {
                this.uiCheckboxRefreshContainers.Checked = Globals.AutoRefreshContainersSetting.ToLower() == "true" ? true : false;
            }

            if (Globals.AutoRefreshBlobsSetting != null)
            {
                this.uiCheckboxRefreshBlobs.Checked = Globals.AutoRefreshBlobsSetting.ToLower() == "true" ? true : false;
            }

            this.uiButtonCancel.Click += new EventHandler(uiButtonCancel_Click);
            this.uiButtonSave.Click += new EventHandler(uiButtonSave_Click);
            this.uiButtonSelectPath.Click += new EventHandler(uiButtonSelectPath_Click);
        }

        void uiButtonSelectPath_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog dialog = new FolderBrowserDialog())
            {
                dialog.RootFolder = Environment.SpecialFolder.MyComputer;

                if (Globals.RootPathSetting != null)
                {
                    dialog.SelectedPath = Globals.RootPathSetting;
                }

                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    this.uiTextboxRootPath.Text = dialog.SelectedPath;
                }
            }
        }

        void uiButtonSave_Click(object sender, EventArgs e)
        {
            try
            {

                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                if (config.AppSettings.Settings[Globals.DefaultAccountSettingName] == null)
                {
                    config.AppSettings.Settings.Add(Globals.DefaultAccountSettingName, this.uiComboboxAccounts.SelectedItem.ToString());
                }
                else
                {
                    config.AppSettings.Settings[Globals.DefaultAccountSettingName].Value = this.uiComboboxAccounts.SelectedItem.ToString();
                }

                if (config.AppSettings.Settings[Globals.AutoRefreshContainersSettingName] == null)
                {
                    config.AppSettings.Settings.Add(Globals.AutoRefreshContainersSettingName, this.uiCheckboxRefreshContainers.Checked.ToString());
                }
                else
                {
                    config.AppSettings.Settings[Globals.AutoRefreshContainersSettingName].Value = this.uiCheckboxRefreshContainers.Checked.ToString();
                }

                if (config.AppSettings.Settings[Globals.AutoRefreshBlobsSettingName] == null)
                {
                    config.AppSettings.Settings.Add(Globals.AutoRefreshBlobsSettingName, this.uiCheckboxRefreshBlobs.Checked.ToString());
                }
                else
                {
                    config.AppSettings.Settings[Globals.AutoRefreshBlobsSettingName].Value = this.uiCheckboxRefreshBlobs.Checked.ToString();
                }

                if (config.AppSettings.Settings[Globals.RootPathSettingName] == null)
                {
                    config.AppSettings.Settings.Add(Globals.RootPathSettingName, this.uiTextboxRootPath.Text);
                }
                else
                {
                    config.AppSettings.Settings[Globals.RootPathSettingName].Value = this.uiTextboxRootPath.Text;
                }

                config.Save(ConfigurationSaveMode.Modified);

                ConfigurationManager.RefreshSection("appSettings");

                this.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void uiButtonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void InitializeAccounts()
        {
            try
            {
                string[] accounts = ConfigurationManager.AppSettings["accounts"].Split('|');
                this.uiComboboxAccounts.Items.Clear();

                foreach (string account in accounts)
                {
                    this.uiComboboxAccounts.Items.Add(account);
                }

                if (Globals.DefaultAccountSetting != null)
                {
                    this.uiComboboxAccounts.SelectedItem = Globals.DefaultAccountSetting;
                }
                else
                {
                    this.uiComboboxAccounts.SelectedIndex = 0;
                }
                
            }
            catch (Exception ex)
            {
            }
        }
    }
}
